package com.grandtech.insurance.common.core.coding;


import com.grandtech.insurance.common.utils.trans.SimpleMap;

import java.util.Iterator;
import java.util.Map;

public final class VectorTileFilter {

    public static byte[] filterVectorTile(byte[] data, SimpleMap<String, Object> map) {
        if (data == null) return null;
        if (map == null || map.size() == 0) return data;

        VectorTileEncoder vectorTileEncoder = new VectorTileEncoder();
        VectorTileDecoder.Feature feature;
        try {
            VectorTileDecoder vectorTileDecoder = new VectorTileDecoder();
            VectorTileDecoder.FeatureIterable iterable = vectorTileDecoder.decode(data);
            Iterator<VectorTileDecoder.Feature> iterator = iterable.iterator();
            Map<String, Object> attribute;
            boolean isCondition;
            while (iterator.hasNext()) {
                isCondition = true;
                feature = iterator.next();
                attribute = feature.getAttributes();
                //System.out.println(attribute);
                /*for (String key : map.keySet()) {
                    if (map.get(key) != null && attribute.get(key) != null) {
                        if (!map.get(key).toString().equals(attribute.get(key).toString())) {
                            isCondition = false;
                            break;
                        }
                    }
                }*/
                if (isCondition) {
                    vectorTileEncoder.addFeature(feature);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return vectorTileEncoder.encode();
    }

}
